package com.itheima.mall.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.mall.dto.ProductDto;
import com.itheima.mall.mapper.ProductMapper;
import com.itheima.mall.pojo.*;
import com.itheima.mall.service.PmsProductService;
import com.itheima.mall.service.PmsSkuStockService;
import com.itheima.mall.vo.PageResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class PmsProductServiceImpl implements PmsProductService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private PmsSkuStockService pmsSkuStockService;
    //添加商品
    @Override
    public void createProduct(ProductDto productParam) {
        Product product = new Product();
        BeanUtils.copyProperties(productParam , product);
        productMapper.insert(product);
        List<SkuStock> skuStockList = productParam.getSkuStockList();
        List<MemberPrice> memberPriceList = productParam.getMemberPriceList();
        List<ProductAttributeValue> productAttributeValueList = productParam.getProductAttributeValueList();
        List<Subject> subjectProductRelationList = productParam.getSubjectProductRelationList();
        List<ProductFullReduction> productFullReductionList = productParam.getProductFullReductionList();
        List<ProductLadder> productLadderList = productParam.getProductLadderList();

        if (CollUtil.isNotEmpty(skuStockList)) {
            skuStockList= skuStockList.stream().map(item ->{
                item.setSkuCode("202002250035004");
                item.setProductId(Long.valueOf(product.getId()));
                return item;
            }).collect(Collectors.toList());
            pmsSkuStockService.saveBatch(skuStockList);
        }

    }



    @Override
    public PageResult selectList(Integer brandId, String keyword, Integer pageNum, Integer pageSize,Integer productCategoryId, String productSn, Integer publishStatus, Integer verifyStatus) {
       //分页构造器
        Page<Product> page = new Page<>(pageNum, pageSize);
        //条件构造器
        LambdaQueryWrapper<Product> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq( brandId != null ,Product::getBrandId , brandId);
        lambdaQueryWrapper.like(keyword != null , Product::getKeywords , keyword);
        lambdaQueryWrapper.like(productSn != null , Product::getProductSn , productSn);
        lambdaQueryWrapper.eq( productCategoryId != null ,Product::getProductCategoryId , productCategoryId);
        lambdaQueryWrapper.eq( publishStatus != null ,Product::getPublishStatus , publishStatus).eq(verifyStatus != null ,Product::getVerifyStatus , verifyStatus)
                .eq(Product::getDeleteStatus,0);
        Page<Product> pageInfo = productMapper.selectPage(page, lambdaQueryWrapper);

        return new PageResult(pageNum , pageSize , (int) pageInfo.getTotal() , pageInfo.getRecords());
    }
    //根据商品名称或货号模糊查询
    @Override
    public List<Product> simpleListByKeyword(String keyword) {
        LambdaQueryWrapper<Product> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(keyword != null , Product::getKeywords , keyword);
        List<Product> productList = productMapper.selectList(lambdaQueryWrapper);
        return productList;
    }
    //批量修改删除状态
    @Override
    public void deleteStatusByIds(Integer deleteStatus, Integer [] ids) {
        LambdaUpdateWrapper<Product> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.in(Product::getId , ids);
        Product product = new Product();
        product.setDeleteStatus(Long.valueOf(deleteStatus));
        productMapper.update(product , lambdaUpdateWrapper);

    }
    //批量设置为新品
    @Override
    public void updateNewStatusByIds(Integer newStatus, Integer[] ids) {
        LambdaQueryWrapper<Product> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(Product::getId , ids);
        Product product = new Product();
        product.setNewStatus(Long.valueOf(newStatus));
        productMapper.update(product , lambdaQueryWrapper);
    }
    //批量上下架商品
    @Override
    public void updatePublishStatusByIds(Integer publishStatus, Integer[] ids) {
        LambdaQueryWrapper<Product> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(Product::getId , ids);
        Product product = new Product();
        product.setPublishStatus(Long.valueOf(publishStatus));
        productMapper.update(product , lambdaQueryWrapper);
    }
    //批量推荐商品
    @Override
    public void updateRecommendStatusByIds(Integer recommendStatus, Integer[] ids) {
        LambdaQueryWrapper<Product> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(Product::getId , ids);
        Product product = new Product();
        product.setRecommandStatus(Long.valueOf(recommendStatus));
        productMapper.update(product , lambdaQueryWrapper);
    }
    //批量修改审核状态 注意
    @Override
    public void updateVerifyStatusByIds(Integer verifyStatus, Integer[] ids, String detail) {
        LambdaQueryWrapper<Product> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(Product::getId , ids);
        Product product = new Product();
        product.setVerifyStatus(Long.valueOf(verifyStatus));
        productMapper.update(product , lambdaQueryWrapper);
    }
    //根据id 修改商品
    @Override
    public void updateById(Integer id , @RequestBody Product productParam) {
        productParam.setId(Long.valueOf(id));
        productMapper.updateById(productParam);
    }

    @Override
    public  Product  updateInfoById(Integer id) {
        Product product = productMapper.selectById(id);
        return product;

    }

    //根据ids 获得商品信息map集合
    @Override
    public List<Product> getProductByIds(List<Long> ids) {
        LambdaQueryWrapper<Product> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Product::getId,ids);

        return productMapper.selectList(queryWrapper);
    }


}
